package com.maltys.tree;

import com.maltys.tree.base.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author malty
 * @link <a href="https://leetcode.cn/problems/validate-binary-search-tree/">...</a>
 */
public class ValidateBinarySearchTree {
    public boolean isValidBST(TreeNode root) {
        List<Long> list = new ArrayList<>();
        traverse(root, list);
        Long cur = Long.MIN_VALUE;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i) <= cur) {
                return false;
            }
            cur = list.get(i);
        }
        return true;
    }


    private void traverse(TreeNode root, List<Long> list) {
        if (root == null) {
            return;
        }

        traverse(root.left, list);
        list.add(root.val + 0L);
        traverse(root.right, list);
    }
}
